##################################################################

# Gautam Nair
# gautam.nair@yale.edu
# Misperceptions of Relative Affluence and Support for International Transfers
# Make Table 5: Mechanisms Social Desirability and Norms of Giving

##################################################################

# setwd("")

##################################################################
# loading packages
##################################################################

library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(ri)
library(dplyr)
library(plyr)
library(scales)

##################################################################

rm(list=ls())

##################################################################
# Treatment Effects: Agricultural Trade Protections
##################################################################

data.working <- readRDS("d_r_cleaned_analysis_dataset.Rda")
dep.var <- c("q12","q14")

ind.group <- c("group3" , "group2")

reg.models <-vector("list", length(dep.var)) 
se.models <-vector("list", length(dep.var))
matrix <- vector("list", length(dep.var))

x=0 

for(i in 1:length(dep.var)){
	
	my.formula <-paste(dep.var[i],'~', paste(ind.group, collapse= ' + '))
	temp.model <- lm(my.formula, data=data.working)
	temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
	x = x+1
	se.models[[x]] <- temp.se
	reg.models[[x]] <- temp.model

}

varlabels <-       c("Information", "Salience", "Constant (Control Mean)" )
spectitle <- c("Mechanisms")
outputfile <- c("tf_t_05_mechanisms.txt")
depvarcaption <- c("Mechanisms")
cutoffs.star <- c(0.1, 0.02)
char.star = c("**", "***")
columnlabels <-c("Social Desirability (1/0)", "Norms of Giving (Ordinal 1-5)")

data.subset <- data.working[data.working$group2==1 | data.working$group3==1,] 
data.subset <- data.working[data.working$group2==1 | data.working$group3==1,] 
tresults.1 <- round((t.test(q12~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.2 <- round((t.test(q14~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
lines.add <- list(c("Information-Salience (p-val.)", tresults.1, tresults.2))


stargazer(reg.models[[1]], reg.models[[2]], 
          se=list(se.models[[1]], se.models[[2]]),
         type="text",
         title= spectitle,
          out=outputfile,
          no.space=TRUE, 
          model.numbers= TRUE,
         notes.append=FALSE,
          align=FALSE,
         covariate.labels=varlabels,
          header= FALSE,
          font.size="small",
          model.names=TRUE,
          digits=2,
          omit.stat = c("rsq", "f","adj.rsq"),
          column.labels  = columnlabels,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
                  star.cutoffs = cutoffs.star,
     star.char = char.star,
         add.lines=lines.add
)   